 

\documentclass[a4paper]{article}

\usepackage{graphicx}
\usepackage[usenames,dvipsnames]{color}
%\usepackage[swedish]{babel}
\usepackage[utf8]{inputenc}

\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhead[R]{Niclas Unnervik, Stefan Johansson, Thomas Lidén}
\fancyhead[L]{}

\usepackage{fancyvrb}       	% FÃ¶r programlistor med tabulatorer
\fvset{tabsize=4}           	% Tabulatorpositioner
\fvset{fontsize=\small}     	% Lagom storlek fÃ¶r programlistor

\title{\begin{center}
Puny news\\eda031 \end{center}}
\author{Niclas Unnervik (dt08nu0), David Everlöf (dt08de2), Thomas Lidén (dt08tl0)}
\date{}       	% Blir dagens datum om det utelÃ¤mnas

\begin{document}            	% BÃ¶rjan pÃ¥ dokumentet

\maketitle                  	% Skriver ut rubriken som vi
                           	% definierade ovan med \title, \author
                           	% och eventuellt \date

\newpage
\large
\tableofcontents
\newpage
% HÃ¤r skrivs all text i dokumentet

\section{System description}
The system consist of two main parts, \emph{the server} and \emph{the client}.

\subsection{News server}
The class newsserver handles new and old connections (via the class \texttt{server} and the class \texttt{connection}) and it keeps a pointer to an instance of the \texttt{database} class and also keeps a \texttt{messagehandler} instance. The \texttt{newsserver} class contains a method \texttt{run()} that contains a blocking call to the method \texttt{waitForActivity()} in \texttt{server}, each time an activity is recieved it means that something has been written to the connection and \texttt{newsserver} then checks whether it is a new connection or an old one. If the connection is new it is registered as a connection. If it is an old one it reads the first byte (since this should be a command) and checks if it is a command. If it is the command is handled by the \texttt{messagehandler} class that parses the command parameters and also answers the client. If it is not a command, an \texttt{ProtocolException} is thrown and the server disconnects the client. This also happens if \texttt{messagehandler} detects a protocol error.

\subsection{Message handler}
The class \texttt{messagehandler} handles (sends, verifies and recieves) messages between the client and the server and the other way around. It contains methods such as \texttt{void ans\_delete\_group(Connection *conn, bool removed)} and \texttt{bool delete\_group(Connection *conn, unsigned int id)}. Each type of command is represented in this way (with two methods, one for use in the client and one for use in the server). The non ans-versions are used by the client and sends commands and parameters to a connection, it takes neccesary strings and numbers as parameters. It also verifies the servers answer, and takes care of the data delivered by from the server (for example an article). The ans-versions is the server side functions, they verify the clients command and sends replies. The class contains some auxilliary methods, such as \texttt{send\_uint(conn, unsigned int)} etc. that makes the sending of more advanced instructions easier.

\subsection{Database}
The \texttt{Database} interface specifies the behaviour of the servers database and there exist two versions of this databse, one stores data in memory, the other on disc. The database contains methods for adding, removing and listing news groups and articles. The \texttt{newsserver} class contains a pointer to the current database. 

\subsection{News client}
The client consists of the \texttt{newsclient} class that has a \texttt{connection} object and a \texttt{messagehandler} object. When connected it waits for user input such as ln (list news groups), la (list articles) etc. if the command requires extra information this will be asked for by the client, for example: the la command requires a group id, so the id has to be given also, this is then sent to the server using the message handler. All calls to the message handler that is supposed to shoe some data from the server returns this information. The \texttt{list\_art(connection*, unsigned int)}-call to the messagehandler will for example return a vector containing pairs with id's and strings that were in the servers answer.

\subsection{Protocol exception}
The \texttt{ProtocolException} struct contains an (hopefully) informative error message, a recieved code and a reference code.

\section{A simple interaction}
An example of how the messagehandler class works with the database and the server is the following:
The news server recieves an action that is read from the connection and is identified as a \texttt{COM\_LIST\_ART}. Since the list article command always should look like
 this: \texttt{COM\_LIST\_ART num\_p COM\_END} the server then calls for the \texttt{read\_uint(Connection*)} method in the message handler that reads and verifies the
 numeric parameter. After this a vector<pair<unsigned int, string> > is created and the group with id corresponding to the numeric parameter is listed in the vector. After
 this the method \texttt{ans\_list\_art(Connection*, vector<pair<unsigned int, string> >)} in the message handler is called, where the \texttt{COM\_END} is first verified,
 then the answer is written. The answer should be structured as the following: \texttt{ANS\_LIST\_ART [ANS\_ACK num\_p [num\_p string\_p]* | ANS\_NAK ERR\_NG\_DOES\_NO
T\_EXIST] ANS\_END} so firstly \texttt{ANS\_LIST\_ART}  is sent using \texttt{send\_code(connection*, char)}, then the vector is checked; if it is contains only one pair
 with id 0, and an empty string, the database cold not find a group with the requested id and \texttt{ANS\_NAK ERR\_NG\_DOES\_NOT\_EXIST ANS\_END} is sent using \texttt{send\_code(connection*, char)}. If this is not the case \texttt{ANS\_ACK} is sent and the size of the vector is sent using \texttt{send\_uint(connection*, unsigned int)} (this
 is the textt{num\_p} part). After this the unsigned int and the string in all pairs is sent using \texttt{send\_uint(connection*, unsigned int)} and \texttt{send\_strin
g(connection*, string)} (this is \texttt{[num\_p string\_p]*}). Finally \texttt{ANS\_END} is sent using \texttt{send\_code(connection*, char)}.

\section{Conclusion}


\newpage
%\bibliographystyle{plain}	% (uses file "plain.bst")
%\bibliography{bibtex}		% expects file "myrefs.bib"

\end{document}              	% Slut på dokumentet




